package controller;

import com.github.pagehelper.PageInfo;
import domain.Programmer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import service.ProgrammerService;

@Controller
@RequestMapping("/programmer")
public class ProgrammerController {

    @Autowired
    private ProgrammerService programmerService;

    //查询所有程序员
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Programmer> allProgrammer = programmerService.findAllProgrammer(pageNum, pageSize);
        modelAndView.getModel().put("page",allProgrammer);
        modelAndView.setViewName("programmer_all");
        return modelAndView;
    }

    //新增程序员
    @RequestMapping("/add")
    public String add(Programmer programmer){
        programmerService.addProgrammer(programmer);
        return "redirect:/programmer/all";
    }

    //修改信息
    @RequestMapping("/update")
    public String update(Programmer programmer){
        programmerService.updateProgrammer(programmer);
        return "redirect:/programmer/all";
    }

    //修改前获取信息
    @RequestMapping("/information")
    public ModelAndView information(int id){
        Programmer programmerById = programmerService.findProgrammerById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.getModel().put("programmer",programmerById);
        modelAndView.setViewName("programmer_update");
        return modelAndView;
    }

    //开启和禁用
    @RequestMapping("/updateStatus")
    public String updateStatus(int[] id,boolean status){
        programmerService.updateProgrammerStatus(id,status);
        return "redirect:/programmer/all";
    }


}
